package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Dept;
import com.qf.bean.Menu;
import com.qf.bean.Role;
import com.qf.dao.DeptMapper;
import com.qf.dao.MenuMapper;
import com.qf.dao.RoleMapper;
import com.qf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * IntelliJ IDEA -> person
 * 2019/11/20 21:17
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Service

public class RoleServiceImpl  implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private MenuMapper menuMapper;

@Transactional
    @Override
    public Map<String,Object> setRoleStateDelete(int[] roleids,Integer state) {
    HashMap<String, Object> map = new HashMap<>();
    map.put("deleteLen",-2);//设置默认值为-2 ，全部的角色都有用户使用
    List<String> rnamelList = new ArrayList<>();
        for (int roleid : roleids) {
            int len = roleMapper.selectEmpByRoleid(roleid);
            if (len > 0){
                String rname = roleMapper.selectrnameByRoleid(roleid);
                rnamelList.add(rname);
            }
            else {
                //假删除
                Map map1 = new HashMap();
                map1.put("roleid",roleid);
                map1.put("state",state);
                int len1 = roleMapper.setRoleStateDelete(map1);
                map.put("deleteLen",len1);
            }
         }
    map.put("rnamelList",rnamelList);
        return  map ;
    }



    @Override
    public List<Role> getallrolesByDid(int deptid) {
        return  roleMapper.getallrolesByDid(deptid);
    }

    @Override
    public PageInfo<Role> findAllRoles(int pageIndex, int pageSize, String rname, String deptname, Integer  state) {
        PageHelper.startPage(pageIndex,pageSize);
        HashMap map = new HashMap();
        map.put("rname",rname);
        map.put("deptname",deptname);
      map.put("state", state);
        List<Role> roles = roleMapper.findAllRoles(map);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return  pageInfo;
    }
@Transactional
    @Override
    public int insertRole(Role role, int[]  menuids) {
    int len = roleMapper.insertSelective(role);
    Integer roleid = role.getRoleid();
    HashMap map = new HashMap();
    map.put("roleid",roleid);
    map.put("menuids",menuids);
    return roleMapper.insertMiddle(map);
}

    @Override
    public Role findRoleAndMenuById(Integer roleid) {
        Role role = roleMapper.selectByPrimaryKey(roleid);
        if (role != null ){
            List<Integer> menuidsList = menuMapper.selectByRoleIdList(roleid);
            role.setMenuidsList(menuidsList);
        }
        return  role;
    }
@Transactional
    @Override
    public int updateRole(Role role, int []menuids) {
//    System.out.println("*********"+role.getMenuidsList());
        int  roleid = role.getRoleid();
        Map map = new HashMap();
        map.put("roleid",roleid);
        map.put("menuids",role.getMenuidsList());
    int len = roleMapper.deleteMiddleMenuRole(map);
    Map map1 = new HashMap();
    map1.put("roleid",roleid);
    map1.put("menuids",menuids);
    int len1 = roleMapper.insertMiddleMenuRole(map1);
    int len3 = roleMapper.updateByPrimaryKeySelective(role);
    return len3;
    }

    @Override
    public int deleteRoleById(Integer roleid) {
        int len = roleMapper.selectEmpByRoleid(roleid);
        if (len > 0){
            return  -2;
        }
        else {
            int len1 = roleMapper.deleteMiddleRole(roleid);
            int len2 = roleMapper.deleteByPrimaryKey(roleid);
            return len2;
        }
    }
}
